﻿using MySql.Data.MySqlClient;
using System;
using System.Data;

namespace DataLayer
{
	public class Connection
	{
		//private string StringConexao = @"SERVER=75.126.154.163; DATABASE=transys; UID=transys; PASSWORD=transys;Convert Zero Datetime=True";
		//private string StringConexao = @"SERVER=sql5c75c.carrierzone.com;DATABASE=transysweb_virtualcad_com_br;UID=virtualcad757733;PASSWORD=Transys2014";
		private string StringConexao = @"SERVER=localhost; DATABASE=db; UID=root; PASSWORD=transys2014";

		private MySqlConnection GetConnection()
		{
			try
			{
				MySqlConnection conn = new MySqlConnection(StringConexao);
				conn.Open();
				return conn;
			}
			catch (Exception)
			{
				throw;
			}
		}

		public string getConnectionString()
		{
			return StringConexao;
		}

		public DataSet Select(string _queryString)
		{
			try
			{
				MySqlDataAdapter adapter = new MySqlDataAdapter();
				adapter.SelectCommand = new MySqlCommand(_queryString, GetConnection());
				DataSet ds = new DataSet();
				adapter.Fill(ds);

				return ds;
			}
			catch (Exception)
			{
				throw;
			}
		}

		public Int64 Insert(string _queryString)
		{
			MySqlCommand cmd = new MySqlCommand(_queryString, GetConnection());
			try
			{
				cmd.ExecuteNonQuery();
				Int64 id = cmd.LastInsertedId;
				cmd.Connection.Close();
				return id;
			}
			catch (Exception)
			{
				cmd.Connection.Close();
				throw;
			}
		}

		public void Update(string _queryString)
		{
			MySqlCommand cmd = new MySqlCommand(_queryString, GetConnection());
			try
			{
				cmd.ExecuteNonQuery();
				cmd.Connection.Close();
			}
			catch (Exception)
			{
				cmd.Connection.Close();
				throw;
			}
		}
	}
}